package com.xiaobange.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaobange.entity.MedicalRecord;
import com.xiaobange.entity.Reservation;
import com.xiaobange.mapper.ReservationMapper;
import com.xiaobange.service.MedicalRecordService;
import com.xiaobange.mapper.MedicalRecordMapper;
import com.xiaobange.utils.ResponseCodeEnum;
import com.xiaobange.vo.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author banji
 * @description 针对表【medical_record(接诊记录表)】的数据库操作Service实现
 * @createDate 2025-04-24 17:57:21
 */
@Service
public class MedicalRecordServiceImpl extends ServiceImpl<MedicalRecordMapper, MedicalRecord>
        implements MedicalRecordService {

    @Autowired
    private MedicalRecordMapper medicalRecordMapper;

    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 添加接诊记录
     *
     * @param medicalRecord 接诊数据
     * @return 接诊结果
     */
    @Override
    public ResultResponse<?> addMedical(MedicalRecord medicalRecord) {
        //判断预约表中是否有数据
        if (medicalRecord.getReservationId() != null) {
            //查询预约表是否存在数据
            Long count = reservationMapper.selectCount(new LambdaQueryWrapper<Reservation>()
                    .eq(Reservation::getReservationId, medicalRecord.getReservationId()));
            if (count > 0){
                //修改接诊的状态
                Reservation reservation = new Reservation();
                reservation.setReservationId(medicalRecord.getReservationId());
                reservation.setStatus("3");
                reservation.setUpdateTime(LocalDateTime.now());
                reservation.setUpdateBy(StpUtil.getLoginId().toString());
                reservationMapper.updateById(reservation);
            }
        }
        //添加接诊数据
        int insert = medicalRecordMapper.insert(medicalRecord);
        if (insert < 0) {
            ResultResponse.error(ResponseCodeEnum.SERVER_ERROR.getCode(), "添加接诊数据失败!");
        }
        return ResultResponse.success(ResponseCodeEnum.NO_CONTENT);
    }
}




